/*
 * defineDeadline.cpp
 *
 *  Created on: 13/02/2014
 *      Author: mattia
 */

#include "signatures.h"
#include <stdio.h>


// Se asume que los tiempos estas ordenados de mayor a menor (como en el paper)
int defineDeadline(int n, int * numP, int ** P, int * M, int ** times, float theta, int & T){

	int T_min = 0, T_max = 0;

	int maxTimes[n];
	int minTimes[n];

	int minPreds, maxPreds;

	for (int act = 0; act < n; act++){

		maxPreds = minPreds = 0;

		for (int i = 0; i < numP[act]; i++){

			if(maxTimes[P[act][i]] > maxPreds){

				maxPreds = maxTimes[P[act][i]];

			}

			if(minTimes[P[act][i]] > minPreds){

				minPreds = minTimes[P[act][i]];

			}
		}

		maxTimes[act] = maxPreds + times[act][0];
		minTimes[act] = minPreds + times[act][M[act] - 1];

		if (maxTimes[act] > T_max){

			T_max = maxTimes[act];

		}

		if(minTimes[act] > T_min){

			T_min = minTimes[act];

		}

	}

	T = T_min + theta * (T_max - T_min);

	return 0;
}
